/**
 * 
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IAttendanceDao;
import com.qiyou.entity.Activity;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * Attendance数据库操作实现
 * 
 * @author 赛艳志
 *
 */
public class AttendanceDaoImpl implements IAttendanceDao {
	private Connection conn = null;
	
	public AttendanceDaoImpl(Connection conn){
		this.conn = conn;
	}
	
	@Override
	public boolean doCreate(String userId, long acId) throws SQLException {
		String sql = "insert into tb_attendance(userID,acID) values(?,?)";
		Object[] params = { userId, acId };
		
        return DBUtil.execute(conn, sql, params);
	}

	@Override
	public boolean doDelete(String userId, long acId) throws SQLException {
		String sql = "delete from tb_attendance where userID=? and acID=?";
		Object[] params = { userId, acId };
		
        return DBUtil.execute(conn, sql, params);
	}

	@Override
	public List<Activity> getUserActivities(String userId) throws SQLException {
		String sql = "select tb_activity.* from tb_activity,tb_attendance where tb_activity.acID=tb_attendance.acID and tb_attendance.userID=?";
		Object[] params = { userId };
		
		List<Activity> result = null;
		
		List<Object[]> all = DBUtil.query(conn, sql, params);
        if (all.size() > 0) {
            result = new ArrayList<Activity>();
            for (Object[] temp : all) {
                Activity activity = new Activity();
                activity = (Activity) TransformUtil.arrayToBean(temp,
                        Activity.class);
                result.add(activity);
            }
        }
        
        return result;
	}

}
